//
//  ZYCVideoPlayerView.m
//  ZDSC
//
//  Created by ZDTX on 2021/9/26.
//

#import "ZYCVideoPlayerView.h"
#import <SJVideoPlayer/SJVideoPlayer.h>


@interface ZYCVideoPlayerView ()

@property (nonatomic, strong) UIView *playerView;


@end
@implementation ZYCVideoPlayerView

- (void)playWithUrlStr:(NSString *)urlStr{
    [self playWithUrlStr:urlStr title:@""];
}

- (void)playWithUrlStr:(NSString *)urlStr title:(NSString *)title{
    if (urlStr.length == 0) {
        return;
    }
    self.player = SJVideoPlayer.player;
    self.player.defaultEdgeControlLayer.hiddenBackButtonWhenOrientationIsPortrait = YES;
    self.player.defaultEdgeControlLayer.hiddenTitleItemWhenOrientationIsPortrait = YES;
    SJVideoPlayerURLAsset * asset = [[SJVideoPlayerURLAsset alloc] initWithURL:[NSURL URLWithString:urlStr]];
    self.player.URLAsset = asset;
    if (title.length > 0) {
        asset.title = title;
    }
    [self.playerView addSubview:self.player.view];
    [self.player.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0.f);
    }];
    self.player.resumePlaybackWhenPlayerHasFinishedSeeking = YES;
    self.player.pauseWhenAppDidEnterBackground = NO;
    self.player.resumePlaybackWhenAppDidEnterForeground = YES;
    [self.player play];
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.playerView];
        [self.playerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.offset(0.f);
            make.height.mas_equalTo(210);
        }];
    }
    return self;
}

- (UIView *)playerView {
    if (!_playerView) {
        _playerView = [[UIView alloc] init];
        _playerView.backgroundColor = [UIColor blackColor];
    }
    return _playerView;
}

-(SJVideoPlayer *) player{
    if (!_player) {
        _player = [SJVideoPlayer player];
        _player.defaultEdgeControlLayer.hiddenBottomProgressIndicator = YES;
        _player.shouldTriggerRotation = ^BOOL(__kindof SJBaseVideoPlayer * _Nonnull player) {
            //如果可以旋转, 则返回YES, 否之
            return YES;
        };
        //取消控制层上下视图的阴影
        [_player.defaultEdgeControlLayer.topContainerView cleanColors];
        [_player.defaultEdgeControlLayer.bottomContainerView cleanColors];
        _player.defaultEdgeControlLayer.hiddenBottomProgressIndicator = YES;
   
        // iOS 14 隐藏画中画
        if (@available(iOS 14.0, *)) {
                _player.defaultEdgeControlLayer.automaticallyShowsPictureInPictureItem = NO;
            }
        _player.pauseWhenAppDidEnterBackground = NO;//默认值为 YES, 即进入后台后 暂停.
        // 是否竖屏时隐藏返回按钮
        _player.defaultEdgeControlLayer.hiddenBackButtonWhenOrientationIsPortrait = YES;
        _player.rotationManager.autorotationSupportedOrientations = SJOrientationMaskAll;

    }
    return _player;
}

@end
